package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * 该类用于处理 /blog 路径对应的请求
 */
@WebServlet("/blog")
@SuppressWarnings({"all"})
public class BlogServlet extends HttpServlet {

    private ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 用于获取数据库中的博客列表、博客详情
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        BlogDao blogDao = new BlogDao();
        // 尝试获取请求中的 blogId, 根据结果区分访问详情页还是博客列表
        String param = req.getParameter("blogId");
        if (param == null){
            // 访问博客列表
            // 从数据库中直接查询博客列表, 然后转成 JSON格式 返回即可
            List<Blog> blogs = blogDao.selectAll();
            // 把 blogs 对象转成 JSON 格式
            String respJson = objectMapper.writeValueAsString(blogs);
            resp.getWriter().write(respJson);
        } else {
            int blogId = Integer.parseInt(param);
            Blog blog = blogDao.selectOne(blogId);
//            System.out.println(blog.getContent());
            String respJson = objectMapper.writeValueAsString(blog);
            resp.getWriter().write(respJson);
        }
    }

    /**
     * 用于实现博客编辑和发布的功能, 发布后跳转到博客列表页
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            // 未登录不能发布博客
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前未登录!");
            return;
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            // 未登录不能发布博客
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前未登录!");
            return;
        }
        // utf8 的编码形式解析请求
        req.setCharacterEncoding("utf8");
        // 发布博客
        Blog blog = new Blog();
        String title = req.getParameter("title"); // 博客标题
        String content = req.getParameter("content");// 博客正文
        // 参数不全的情况
        if (title == null || "".equals(title) || content == null || "".equals(content)) {
            // 反馈客户端参数不全
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("参数不全, 无法发布博客");
            return;
        }
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(((User) session.getAttribute("user")).getUserId()); // 作者信息
        new BlogDao().insert(blog); // 发布
        // 跳转
        resp.sendRedirect("blog_list.html");
    }
}
